<?php
/**
 * Created by PhpStorm.
 * User: N
 * Date: 2019/5/30
 * Time: 13:28
 */

namespace app\index\controller;


use app\index\model\doGoods;
use app\index\model\doUser;
use think\Db;
use think\facade\Request;


class User extends Base
{
    public function initialize()
    {
        parent::initialize();
    }

    public function index(){
        $admin = Db::name("admin")->where("id",$this->admin['id'])->find();
        if($admin['if_rz']==0){
             echo "<script>location.href='/index/index/renzheng'</script>";die;
        }
        if(Request::isPost()){
            $id = input('id');
            $data = input("post.");
            // dump();
            // if(strtotime($data['work_etime'])<strtotime($data['work_stime'])){
            //     ajaxMsg(0,'教师工作年限有误');
            // }
            // if($data['schoolstyle']==2||$data['schoolstyle']==68){
            //     $data['area'] = $data['school'];
            //     $data['school'] = $data['aschool'];
            //     // unset($data['aschool']);
            // }else{
            //     $data['area'] = 1;
            // }
            if(!in_array('无',$data['zhizheng'])){
                if(isset($data['zsfield'])){
                    if(count($data['zsfield'])==0){
                         ajaxMsg(0,'请上传证书文件！');
                    }
                }else{
                     ajaxMsg(0,'请上传证书文件！');
                }
            }
            if(isset($data['zsfield'])){
                $data['zsfield'] = json_encode($data['zsfield']);
                $data['zsop'] = json_encode($data['zsop']);
            }
            
            //unset($data['schoolstyle']);
            unset($data['aschool']);unset($data['file']);
            $w_time = strtotime($data['work_etime'])-strtotime($data['work_stime']);
         
            $data['zhizheng'] = implode(',',$data['zhizheng']);
            // unset($data['work_stime']);unset($data['work_etime']);
            
            $data['xueduan'] = json_encode($data['xueduan']);

            $w_time = $this->Sec2Time($w_time);
            $data['ruzhi'] = $w_time;
            $data['admin_id'] = $this->admin['id'];
    
            $gongzuo = [];
            if(isset($data['w_stime'])){
                foreach ($data['w_stime'] as $key=>$val){
                    $gongzuo[$key]['w_stime'] = $val;
                    $gongzuo[$key]['w_etime'] = $data['w_etime'][$key];
                    if(isset($data['w_danwei'][$key]))$gongzuo[$key]['w_danwei'] = $data['w_danwei'][$key];
                    else $gongzuo[$key]['w_danwei'] = '空';
                    if(isset( $data['w_jingli'][$key]))$gongzuo[$key]['w_jingli'] = $data['w_jingli'][$key];
                    else $gongzuo[$key]['w_jingli'] = '空';
                }
                unset($data['w_stime']);unset($data['w_etime']);unset($data['w_danwei']);unset($data['w_jingli']);
        
                $data['work'] = json_encode($gongzuo);
            }
            $gongzuo = [];
            if(isset($data['p_stime'])){
                foreach ($data['p_stime'] as $key=>$val){
                    $gongzuo[$key]['p_stime'] = $val;
                    $gongzuo[$key]['p_etime'] = $data['p_etime'][$key];
                    if(isset($data['p_danwei'][$key]))$gongzuo[$key]['p_danwei'] = $data['p_danwei'][$key];
                    else $gongzuo[$key]['p_danwei'] = '空';
                    if(isset( $data['p_jingli'][$key]))$gongzuo[$key]['p_jingli'] = $data['p_jingli'][$key];
                    else $gongzuo[$key]['p_jingli'] = '空';
        
                }
                unset($data['p_stime']);unset($data['p_etime']);unset($data['p_danwei']);unset($data['p_jingli']);
                $data['peixun'] = json_encode($gongzuo);
                unset($data['nianxian']);
            }
            $baocun = input("baocun");
            
            if($baocun==1){
                $data['status'] = 1;
            }else{
                $data['status'] = 0;
            }
            // dump($data);die;
            $data['school'] = $this->admin['school'];
            $adta['area'] = $this->admin['area'];
            $res = Db::name("renzheng")->where("id",$id)->update($data);
            if($res){
                ajaxMsg(1,'保存成功');
            }
            ajaxMsg(0,'无变更项目！');
        }
        $member = Db::name("renzheng")->where("admin_id",$this->admin['id'])->find();
        $work = json_decode($member['work'],true);

        $this->assign('work',$work);

        $peixun = json_decode($member['peixun'],true);

        $this->assign('peixun',$peixun);

        $this->assign('admin',$member);
        
        $xueduan = json_decode($member['xueduan'],true);
        $this->assign('xueduan',$xueduan);
        
        $zhizheng = explode(',',$member['zhizheng']);
        $this->assign('zhizheng',$zhizheng);
        // dump($zhizheng);die;
        
        $zsfield = json_decode($member['zsfield'],true);
        $this->assign("zsfield",$zsfield);
        
        $zsop = json_decode($member['zsop'],true);
        // dump($zsop);die;
        $cate = Db::name("cate")->where('status',1)->where('pid','1')->select();
        // dump($member);die;
        $area = Db::name("cate")->where("id",$this->admin['area'])->find();
        $school = Db::name("cate")->where("id",$this->admin['school'])->find();
        $this->assign('zsop',$zsop);
        $this->assign('member',$member);
        $this->assign('area',$area);
        $this->assign('school',$school);
        $this->assign('cate',$cate);
        $zs = Db::name("zhengshu")->where("status",1)->select();
        // dump($zs);die;
        $this->assign("zs",$zs);
        return $this->fetch();
    }
    public function baocun(){
        $id = input('id');
            $data = input("post.");
            // dump($data);die;
//        $data['work_stime'] = "2018-01-08";
            if(strtotime($data['work_etime'])<strtotime($data['work_stime'])){
                ajaxMsg(0,'教师工作年限有误');
            }
            if($data['schoolstyle']==2||$data['schoolstyle']==68){
                $data['area'] = $data['school'];
                $data['school'] = $data['aschool'];
                // unset($data['aschool']);
            }else{
                $data['area'] = 1;
            }
            if(isset($data['zsfield'])){
                $data['zsfield'] = json_encode($data['zsfield']);
            }
            
            //unset($data['schoolstyle']);
            unset($data['aschool']);unset($data['file']);
            $w_time = strtotime($data['work_etime']).'-'.strtotime($data['work_stime']);
            
            $data['zhizheng'] = json_encode($data['zhizheng']);
            // unset($data['work_stime']);unset($data['work_etime']);
            
            $data['xueduan'] = json_encode($data['xueduan']);
            // if(isset($data['other'])){
            //     $data['teacher'] .=$data['other'];
            // }
            // $w_time = $this->Sec2Time($w_time);
            $data['ruzhi'] = $w_time;
            $data['admin_id'] = $this->admin['id'];
            $gongzuo = [];
            foreach ($data['t_stime'] as $key=>$val){
                $gongzuo[$key]['t_stime'] = $val;
                $gongzuo[$key]['t_etime'] = $data['t_etime'][$key];
            }
            unset($data['t_stime']);unset($data['t_etime']);
            $data['pwork'] = json_encode($gongzuo);
    
            $gongzuo = [];
            if(isset($data['w_stime'])){
                foreach ($data['w_stime'] as $key=>$val){
                    $gongzuo[$key]['w_stime'] = $val;
                    $gongzuo[$key]['w_etime'] = $data['w_etime'][$key];
                    if(isset($data['w_danwei'][$key]))$gongzuo[$key]['w_danwei'] = $data['w_danwei'][$key];
                    if(isset( $data['w_jingli'][$key]))$gongzuo[$key]['w_jingli'] = $data['w_jingli'][$key];
                }
                unset($data['w_stime']);unset($data['w_etime']);unset($data['w_danwei']);unset($data['w_jingli']);
        
                $data['work'] = json_encode($gongzuo);
            }
            
            $gongzuo = [];
            if(isset($data['p_stime'])){
                foreach ($data['p_stime'] as $key=>$val){
                    $gongzuo[$key]['p_stime'] = $val;
                    $gongzuo[$key]['p_etime'] = $data['p_etime'][$key];
                    if(isset($data['p_danwei'][$key]))$gongzuo[$key]['p_danwei'] = $data['p_danwei'][$key];
                    if(isset( $data['p_jingli'][$key]))$gongzuo[$key]['p_jingli'] = $data['p_jingli'][$key];
        
                }
                unset($data['p_stime']);unset($data['p_etime']);unset($data['p_danwei']);unset($data['p_jingli']);
                $data['peixun'] = json_encode($gongzuo);
                unset($data['nianxian']);
            }
            $data['status'] = 0;
            $data['school'] = $this->admin['school'];
            $adta['area'] = $this->admin['area'];
            $res = Db::name("renzheng")->where("id",$id)->update($data);
            if($res){
                ajaxMsg(1,'保存成功');
            }
            ajaxMsg(0,'网络错误,稍后再试');
    }
    private function Sec2Time($time){
        if(is_numeric($time)){
            $value = array(
                "years" => 0, "days" => 0, "hours" => 0,
                "minutes" => 0, "seconds" => 0,
            );
            if($time >= 31556926){
                $value["years"] = floor($time/31556926);
                $time = ($time%31556926);
            }
            if($time >= 86400){
                $value["days"] = floor($time/86400);
                $time = ($time%86400);
            }
            if($time >= 3600){
                $value["hours"] = floor($time/3600);
                $time = ($time%3600);
            }
            if($time >= 60){
                $value["minutes"] = floor($time/60);
                $time = ($time%60);
            }
            $value["seconds"] = floor($time);
            //return (array) $value;
            $t=$value["years"] ."年". $value["days"] ."天"." ". $value["hours"] ."小时". $value["minutes"] ."分".$value["seconds"]."秒";
            Return $t;

        }else{
            return (bool) FALSE;
        }
    }
    public function shenhe(){
        $admin = Db::name("admin")->where("id",$this->admin['id'])->find();
        if($admin['if_rz']==0){
             echo "<script>location.href='/index/index/renzheng'</script>";die;
        }
        $list = Db::name("renzheng")->where('admin_id',$this->admin['id'])->select();
        // dump($list);die;
        foreach($list as &$val){
            if($val['xuexi']){
                $val['xuexi'] = implode(',',json_decode($val['xuexi'],true));
            }else{
                $val['xuexi'] = '';
            }
        }
        // dump($list);die;
        $this->assign('list',$list);
        return $this->fetch();
    }
    public function changestatus(){
        $id = input('id');
        $info = Db::name("renzheng")->where('id',$id)->find();
        // dump($info['gid']);die;
        //查看是否为直属学校
        $cate = Db::name("cate")->where("id",$this->admin['school'])->find();
        
        if($cate['pid']==52){
            $res = Db::name("renzheng")->where('id',$id)->update(['status'=>5]);
        }else{
            $res = Db::name("renzheng")->where('id',$id)->update(['status'=>1]);
        }
        
        ajaxMsg('1','提交成功');
    }
    public function message(){
        echo "开发中";
    }
    public function zhengshu(){
        echo "开发中";
    }


}